const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status:0,
        withStatus:1,
        total:0,
        number:0,
        couponNo:[],
        idArray:[],
        showMoney:true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        var open=wx.getStorageSync('open');
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })             
        that.setData({
            platform:app.globalData.platform,    
            open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    goBack:function(){
        wx.navigateBack();
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var open=wx.getStorageSync('open');
       // this.setData        }
       //var date=util.formatTime(new Date());
       //log.info("Time:"+date+",page:mine/wallet/onshow,data:"+JSON.stringify(open));
       if(open.role==0){
        this.getShareMoney();
       }else if(open.role==1){
        this.getShareMoney();
       }else{
        this.getShareMoney();
       }
        
    },
    getShareMoney:function(){
        let that=this;
      //  var status=this.data.status;
        var open=wx.getStorageSync('open');
        var user_id=open.user_id;
     
       //user_id=164255;  
        wx.request({
          url: app.globalData.domain+'member/wallet/wallet',
          data:{user_id:user_id,from:1},
          success:res=>{
             // console.log(res.data);
             var data=res.data;
         
           console.log(data);
             that.setData({
                 total:data.total,
                 no_with:data.no_with,
                 moneyArrays:data.list,
                 moneyArray:data.list,
                 money:data.money,
                 with_money:data.with_money,
                 count:data.count,
                 counts:data.counts
             })
          }
        })
    },
    selectMoneyWith:function(e){
        console.log(e);       
        var data=this.data;
        var  moneyArray= data.moneyArray;
        var id=e.currentTarget.dataset.id;
        var number=data.number;
        var total=data.total;     
        console.log(id);
                var idArray=data.idArray;
        moneyArray.forEach(function(item,index){      
                var money=item.money;   
           
            if(id==item.id){
                if(item.select==1){
                    item.select=0;
                    total=parseInt(total)-parseInt(money);
                    number=number-1;
               
                   // var id=item.id;
                    console.log(id);
                    var ids=idArray.indexOf(id)
                    if (ids > -1) { 
                        idArray.splice(ids, 1); 
                      }
                  
                    
                }else{
                    item.select=1;
                    total = parseInt(total)+parseInt(money);
                    number=number+1;
                   idArray.push(item.id);
                   
                }
                
            }
           
        })
 
        this.setData({
            total:total,
            number:number,
            moneyArray:moneyArray,
            idArray:idArray
   
        })

    },
    shareWith:function(){
        let that=this;
        var open= wx.getStorageSync('open');
        var data=this.data;
        var total=data.money;
        var idArray=[];
        var idArray=data.idArray;
        var moneyArray=data.moneyArray;
        moneyArray.forEach(function(item,index){             
               idArray.push(item.id);              
        })
        var ids=idArray.toString();
        if(total>2000){
            wx.showToast({
              title: '提现金额不能大于2000',
            })
        }else if(total<=0){
            wx.showToast({
                title: '提现金额为零',
              })
        }else{
           
            wx.request({
                url: app.globalData.platUrl+'mine/shareWith',
                data:{user_id:open.user_id,total:total,ids:ids,from:1},
                success:res=>{
                  console.log(res);
                  var data=res.data;
                  if(data.status==200){
                      that.setData({
                        total:0,
                        number:0
                      })
                    wx.showToast({
                      title: '提现成功',
                    })
                    that.getShareMoney();
                  }else{
                    that.setData({
                        total:0,
                        number:0
                    })
                    that.getShareMoney();
                    that.showModels(data.msg,"#"); 
                  }
                }
            })
          //  break;
          
        }
    },
    withAll(){
        let that=this;
        var open= wx.getStorageSync('open');
        var data=this.data;
        var total=data.money;
        var idArray=[];
        var moneyArray=data.moneyArray;
        var withStatus=data.withStatus;
        moneyArray.forEach(function(item,index){             
               idArray.push(item.id);              
        })
        var ids=idArray.toString();
        if(total>2000){
            wx.showToast({
              title: '提现金额不能大于2000',
            })
        }else if(total<=0){
            wx.showToast({
                title: '提现金额为零',
              })
        }else{
            if(withStatus==1){

           
           this.setData({
               withStatus:0
           })
           setTimeout(function () {
            that.setData({
                withStatus: 1
            })
          }, 2000) //阻止一秒点击多次
            console.log('total='+total+'ids=>'+ids);
            wx.request({
                url: app.globalData.platUrl+'mine/shareWith',
                data:{user_id:open.user_id,total:total,ids:ids,from:1},
                success:res=>{
                  console.log(res);
                  var datas=res.data;
                  if(datas  .status==200){
                      that.setData({
                        total:0,
                        number:0
                      })
                    wx.showToast({
                      title: '提现成功',
                    })
                    that.getShareMoney();
                  }else{
                    that.setData({
                        total:0,
                        number:0
                    })
                    that.getShareMoney();
                    that.showModels(datas.msg,datas.url);
                  }
                }
            })
             }else{
                wx.showToast({
                    title: '请请勿重新点击',
                  })
             }
        }
    },
    getWallet:function(){
        let that=this;
        var status=this.data.status;
        var open=wx.getStorageSync('open');
        wx.request({
          url: app.globalData.platUrl+'mine/wallet',
          data:{user_id:open.user_id,status:status,from:1},
          success:res=>{
             // console.log(res.data);
             var data=res.data;
           console.log(data);
             that.setData({
                 role:data.role,
                 with:data.with,
                 coupons:data.coupon,
                 coupon:data.coupon,
                 money:data.money,
                 with_money:data.with_money,
                 count:data.count,
                 counts:data.counts
             })
          }
        })
    },
    getList:function(e){
        console.log(e);
        let data=this.data;
        var status = e.currentTarget.dataset.status;
        var coupon=[];
        var count;
        var coupons=data.coupons;
        if(status==0){
            coupon=coupons;
        }
        if(status==1){
            //coupon=data.coupons;
            coupons.forEach(function(item,index){
                if(item.role==1){
                    coupon.push(item)  
                }
            })
        }
        if(status==2){
            coupons.forEach(function(item,index){
                if(item.role==2){
                    coupon.push(item)  
                }
            })
        }
        if(status==3){
            coupons.forEach(function(item,index){
                if(item.role==0){
                    coupon.push(item)  
                }
            })
        }
        if(status==9){
            coupon = data.with;
            console.log(coupon)
            
        }
        count = coupon.length;
        console.log(count)
        this.setData({
            status:status,
            coupon:coupon,
            count:count
        })
    },
    selectWith:function(e){
        console.log(e);       
        var data=this.data;
        var coupon = data.coupon;
        var id=e.currentTarget.dataset.id;
        var number=data.number;
        var total=data.total;
        var couponNo=data.couponNo;
        var role=data.open.role;
        console.log(id);
        coupon.forEach(function(item,index){
            if(role==1){
                var money=item.money;
            }
            if(role==2){
                var money=item.cloud_money;
            }
            if(id==item.id){
                if(item.select==1){
                    item.select=0;
                    total=parseInt(total)-parseInt(money);
                    number=number-1;
                    var coupon_no=item.coupon_no;
                    console.log(coupon_no);
                    var ids=couponNo.indexOf(coupon_no)
                    if (ids > -1) { 
                        couponNo.splice(ids, 1); 
                      }
                    
                }else{
                    item.select=1;
                    total = parseInt(total)+parseInt(money);
                    number=number+1;
                    couponNo.push(item.coupon_no);
                   
                }
                
            }
           
        })
        console.log(couponNo);
        this.setData({
            total:total,
            number:number,
            coupon:coupon,
            couponNo:couponNo
        })

    },
    goWith:function(){
        let that=this;
        var open= wx.getStorageSync('open');
        var data=this.data;
        var total=data.total;
        var couponNo=data.couponNo;
        var coupon_no=couponNo.toString();
        if(total>2000){
            wx.showToast({
              title: '提现金额不能大于2000',
            })
        }else if(total<=0){
            wx.showToast({
                title: '提现金额为零',
              })
        }else{
            wx.request({
                url: app.globalData.platUrl+'mine/With',
                data:{user_id:open.user_id,total:total,coupon_no:coupon_no,from:1},
                success:res=>{
                  // console.log(res);
                  if(res.data.status==200){
                      that.setData({
                          total:0
                      })
                    wx.showToast({
                      title: '提现成功',
                    })
                    that.getWallet();
                  }else{
                    that.setData({
                        total:0
                    })
                    wx.showToast({
                        title: '失败',
                      }) 
                      that.getWallet(); 
                  }
                }
            })
        }
    },
    showModels:function(msg,url){
        wx.showModal({
          cancelColor: 'cancelColor',
          title:'系统提示',
          content:msg,
          //showCancel:false,
          success:function(e){
            if (e.confirm) {
               if(url!="#"){
                    wx.reLaunch({
                      url: url,
                    })
               }
              }
          }
          

        })

    },
    moneyShow:function(){
      var showMoney=this.data.showMoney;
      if(showMoney){
          this.setData({
              showMoney:false
          })
      }else{
          this.setData({
              showMoney:true
          })
      }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})